package com.robot.dto;

import com.robot.constant.BuildTestStatus;
import com.robot.model.BuildTestCaseDo;
import com.robot.model.BuildTestDo;
import com.robot.model.BuildTestInterfaceDo;
import com.robot.util.ReportLogUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/12/1
 * Time: 11:00
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Data
public class RunTestContext {
    private BuildTestDo buildTestDo;
    private ReportLogUtil reportLogUtil;

    public void setBuildTestDo(BuildTestDo buildTestDo) {
        buildTestDo.setCaseSize(0);
        buildTestDo.setPassCaseSize(0);
        buildTestDo.setFailCaseSize(0);
        this.buildTestDo = buildTestDo;
    }

    private BuildTestInterfaceDo buildTestInterfaceDo;
    private BuildTestCaseDo buildTestCaseDo;
    /**
     * 计算当前运行接口的通过率以及整个运行计划的通过率
     * **/
    public void calculation(List<BuildTestCaseDo> buildTestCaseDoList){
        Integer interfaceTestCaseSize=buildTestCaseDoList.size();
        BuildTestInterfaceDo buildTestInterfaceDo =this.getBuildTestInterfaceDo();
        /**拿到当前运行的接口类,设置用例总数**/
        buildTestInterfaceDo.setCaseSize(interfaceTestCaseSize);
        /**获取用例通过数量**/
        Integer passCaseSize = 0;
        for(BuildTestCaseDo buildTestCaseDo:buildTestCaseDoList){
            if (buildTestCaseDo.getStatus().intValue() == BuildTestStatus.PASS) {
                passCaseSize++;
            }
        }
        buildTestInterfaceDo.setPassCaseSize(passCaseSize);
        Integer failCaseSize = interfaceTestCaseSize - passCaseSize;
        buildTestInterfaceDo.setFailCaseSize(failCaseSize);
        Float caseRate = (float) passCaseSize / (float) buildTestCaseDoList.size() * 100;
        buildTestInterfaceDo.setPassRate(new BigDecimal(caseRate));
        /**设置当前接口的运行状态**/
        if (buildTestCaseDoList.size() == passCaseSize) {
            buildTestInterfaceDo.setStatus(BuildTestStatus.PASS);
        } else if (buildTestCaseDoList.size() - passCaseSize > 0) {
            buildTestInterfaceDo.setStatus(BuildTestStatus.PARTPASS);
        } else {
            buildTestInterfaceDo.setStatus(BuildTestStatus.FAIL);
        }
        //--------------------------------计算整个这次构建的运行信息-----------------------------------
        BuildTestDo buildTestDo =this.getBuildTestDo();
        Integer buildTestDoCaseSize= buildTestDo.getCaseSize();
        buildTestDo.setCaseSize(buildTestDoCaseSize+interfaceTestCaseSize);
        Integer buildTestDoPassCaseSize =buildTestDo.getPassCaseSize();
        buildTestDo.setPassCaseSize(buildTestDoPassCaseSize+passCaseSize);
        buildTestDo.setFailCaseSize(buildTestDo.getFailCaseSize()+failCaseSize);
        Float buildCaseRate = (float) (buildTestDoPassCaseSize+passCaseSize) / (float) (buildTestDoCaseSize+interfaceTestCaseSize) * 100;
        buildTestDo.setPassRate(new BigDecimal(buildCaseRate));
        /**设置当前接口的运行状态**/
        if (buildTestDoCaseSize+interfaceTestCaseSize == buildTestDoPassCaseSize+passCaseSize) {
            buildTestDo.setStatus(BuildTestStatus.PASS);
        } else if (buildTestDoCaseSize+interfaceTestCaseSize - buildTestDoPassCaseSize+passCaseSize > 0) {
            buildTestDo.setStatus(BuildTestStatus.PARTPASS);
        } else {
            buildTestDo.setStatus(BuildTestStatus.FAIL);
        }
    }
}
